<%@ page pageEncoding="UTF-8"%>
<%@ include file="/jsp/adm/inc/incBodyFrameHeader.jsp"%>
    	<script type="text/javascript" src="<c:out value="${gsDomainContext}"/>/script/common/doitBox.js"></script>
    	<script type="text/javascript" src="<c:out value="${gsDomainContext}"/>/script/common/doit_calendar.js"></script>
		<script type="text/javascript" src="<c:out value="${gsDomainContext}"/>/script/common/doitTreeUI.js"></script>
		<script type="text/javascript" src="<c:out value="${gsDomainContext}"/>/script/common/dragdrop.js"></script>	
		<script type="text/javascript" src="<c:out value="${gsDomainContext}"/>/script/common/doitTooltip.js"></script>
			
		<!-- form 시작  -->
		<form id="<c:out value="${gsMainForm}"/>" name="<c:out value="${gsMainForm}"/>" method="post" required>
		<!-- 공통 hidden input 정의  시작 -->
		<!-- 공통 hidden input 정의  끝 -->
		<!-- 목록 페이지로 이동할 때 현재 페이지정보 설정  시작 -->
        <input type="hidden" id="page" name="page" value="<c:out value="${param.page}"/>" />
		<input type="hidden" id="orderBy" name="orderBy" value="<c:out value="${param.orderBy}"/>" />
		<input type="hidden" id="rowsPerPage" name="rowsPerPage" value="<c:out value="${param.rowsPerPage}"/>" />
		<!-- 목록 페이지로 이동할 때 현재 페이지정보 설정  끝 -->
		<%@ include file="/jsp/adm/inc/incHiddenParam.jsp"%>
		<!-- 검색 영역 시작  -->
		<!-- 검색 영역 끝  -->
		<table class="tabletype3" summary="미터정보 표시">
			<caption>미터정보 표시</caption>
			<colgroup>
				<col />
			</colgroup>
			<tbody>
				<tr>
					<th scope="row"><FONT COLOR="RED"><c:out value="${gsServerTitle}"/></FONT>
					<br>
					<br>
					<FONT COLOR="BLUE">
					(운영환경 : spring 2.5 + ibatis 2.0 + JBoss 4.2[Tomcat 5.5] + oracle 10g + jdk 1.5.0_11)
					</FONT>
					</th>
				</tr>
			</tbody>
		</table>
		<BR>
		<!-- 표시부 시작 -->
		<table class="tabletype3" summary="테이블 표시">
		<caption>테이블 정보  표시</caption>
		<colgroup>
			<col class="tw20" />
			<col />
		</colgroup>
		<tbody>
			<tr>
				<th scope="row">시스템구분</th>
				<td class="line">
					<input type="text" id="systemDiv" name="systemDiv" class="txt w309" title="시스템구분" value="<c:out value="${param.systemDiv}" />" />
					&nbsp;&nbsp;<strong><font color="red">example : 수업</font></strong>
				</td>
			</tr>
			<tr>
				<th scope="row">프로퍼티 파일 경로</th>
				<td class="line">
					<input type="text" id="propertyFilePath" name="propertyFilePath" class="txt w309 required" title="프로퍼티 파일 경로" value="<c:out value="${filePath}" />" />
					&nbsp;&nbsp;<strong><font color="red">example : D:\workspace\generator</font></strong>
				</td>
			</tr>
			<tr>
				<th scope="row">패키지 경로</th>
				<td class="line">
					<input type="text" id="pakagePath" name="pakagePath" class="txt w309 required" title="패키지 경로" value="<c:out value="${param.pakagePath}" />" />
					&nbsp;&nbsp;<strong><font color="red">example : pk.adm.code</font></strong>
				</td>
			</tr>
			<tr>
				<th scope="row">JSP 경로</th>
				<td class="line">
					<input type="text" id="jspPath" name="jspPath" class="txt w309 required" title="JSP 경로" value="<c:out value="${param.jspPath}" />" />
					&nbsp;&nbsp;<strong><font color="red">example : /jsp/pk/adm/code/</font></strong>
				</td>
			</tr>
			<tr>
				<th scope="row">관리자용/사용자용</th>
				<td class="line">
					<select class="combo w167 required" id="auth" name="auth" title="JSP 파일 생성">
                   		<c:forEach var="list" items="${userSettingList}" varStatus="status">
                   			<OPTION value="<c:out value="${list.key}" />"><c:out value="${list.value}" />(<c:out value="${list.key}" />)</OPTION>
                   		</c:forEach>
                   	</select>
				</td>
			</tr>
			<tr>
				<th scope="row">도움말 </th>
				<td class="line" style="padding: 5px;" id="help" name="help">
				</td>
			</tr>	
		</tbody>
		</table>
		<div class="boxsearch">
			<dl class="searcha">
				<dt class="tw20">테이블명</dt>
				<dd><input id="search" name="search" type="text" class="txt w309" value="<c:out value="${param.search}" />" /></dd>
				<dd class="btn"><input type="button" value="검색" class="btn3" onClick="javascript:CtlExecutor.doPageList();" /></dd>
			</dl>
		</div>	
		
		<!-- 목록 상단 좌측 명령 버튼 시작  -->
        <div class="btnbox">
            <div class="left">
                <input type="button" value="전체선택" class="btn2" onClick='javascript:commUtil.doCheckAll("<c:out value="${gsMainForm}"/>");' />
            </div>
             <div class="right">
             	 <input type="button" value="조인코드" class="btn2" title="조인코드" onClick="javascript:PopExecutor.doJoinTableSelect();" />
                 <input type="button" value="코드생성" class="btn2" title="코드생성" onClick="javascript:DwrExecutor.doGenerator();" />
             </div>
        </div>
        <!-- 목록 상단 좌측 명령 버튼 끝  -->
		
		<c:set var="_colspan" value="6" />
		
		<table class="boardlist" summary="테이블 리스트입니다.">
            <caption>테이블 목록</caption>
            <!-- 과정 cell width 지정 시작  -->
            <colgroup>
                <col class="tw5" />
                <col class="tw5" />
                <col class="tw25" />
                <col class="tw30" />
                <col class="tw20" />
                <col class="tw15" />
            </colgroup>
            <!-- 목록 cell width 지정 시작  -->
            <!-- 목록 헤더(타이틀) 영역 끝  -->
            <thead>
                <tr>
                    <th class="sta">순번</th>
                    <th>선택</th>
                    <th>테이블명</th>
                    <th>테이블명 설명</th>
                    <th>클래스명</th>
                    <th class="end">JSP파일생성</th>
                </tr>
            </thead>
            <!-- 목록 헤더(타이틀) 영역 끝  -->
            
            <tbody id="dataRow">
            <c:if test="${empty output}">
                <tr>
                    <td class="nodata" colspan="<c:out value="${_colspan}" />">조회 내용이 없습니다.</td>
                </tr>
            </c:if>
            <!-- 목록 ResultSet 반복 영역 시작  -->
            <c:forEach var="list" items="${output}" varStatus="status">
                <tr class="mouseover" id="<c:out value="${list.tableName}"/>" name="<c:out value="${list.tableName}"/>">
                    <td><c:out value="${status.count}"/></td>
                    <td class="btn" style="height: 25px;"><input type="checkbox" id="chk" name="chk" onclick="javascript: commUtil.changeStyle(this);" value="<c:out value="${list.tableName}"/>" /></td>
                    <td class="left"><c:out value="${list.tableName}"/></td>
                    <td class="left"><c:out value="${list.tcomment}"/></td>
                    <td class="left"><input name="CLASS_<c:out value="${list.tableName}" />" id="CLASS_<c:out value="${list.tableName}" />" type="text" value="<c:out value="${list.tableName}"/>" class="txt w120" /></td>
                    <td>
                    	<img src="<c:out value="${gsDomainContext}" />/images/adm/common/icon/wribtn_01.gif" title="컬럼조회" style="cursor: pointer;" onclick="EtcExecutor.doColumnList('td_<c:out value="${status.count}" />', '<c:out value="${list.tableName}"/>');" />
                    </td>
                </tr>
                <tr>
					<td class="inner" id="td_<c:out value="${status.count}" />" colspan="<c:out value="${_colspan}" />" style="width : 100%; display : none;">
					</td>
				</tr>
               	<c:if test="${!status.last}">
                <tr id="<c:out value="${list.tableName}"/>_line" name="<c:out value="${list.tableName}"/>_line">
                    <td class="btline" colspan='<c:out value="${_colspan}" />'></td>
                </tr>
                </c:if>
                <c:if test="${status.last}">
                <tr>
                	<td class="end" colspan='<c:out value="${_colspan}" />' /></td>
                </tr>
                </c:if>
            </c:forEach>
            </tbody>
            <!-- 목록 ResultSet 반복 영역 끝  -->
       </table>            
	</form>
	
	<!-- form 끝  -->
			
<%@ include file="/jsp/adm/inc/incBodyFrameFooter.jsp"%>

<%-- 페이지 스크립트 영역 --%>
<script language="javascript">
	Event.observe(window, 'load', function(){
		var oForm = $$('form');

		<%-- onLoad Script 설정 --%>
		for (var i = 0 ; i < oForm.length ; i++)	{
			if (oForm[i].getAttribute('required') != null) {
				validUtil.onLoad(oForm[i]);
			}
		}
		shortcut.add("Enter", function() {
			CtlExecutor.doPageList();
		});
	});
	//controller 관련 function 정의
    var CtlExecutor = {
        doPageList : function(){
            doFormSubmit( '<c:out value="${gsMainForm}"/>', false, { action: "<c:out value="${gsDomainContext}"/>/table.do", method: ""});
        }
    }
    var DwrExecutor = {
    	doGenerator : function() {
			var userFunction =  function() {
	    		if (commUtil.doCheckItems($("<c:out value="${gsMainForm}"/>"), "chk", "코드생성")) {
	    			var map = new JMap();
	    			var tableName = new Array();
	    			var className = new Array();
	    			var elements = $("<c:out value="${gsMainForm}"/>").elements;
	    			for (var i = 0 ; i < elements.length ; i++) {
		  				if (elements[i].type == 'checkbox' && elements[i].name == 'chk' && elements[i].checked) {
		  					tableName.push(elements[i].value);
		  					className.push($F('CLASS_'+elements[i].value));
		  				}
		  			}
		  			
					map.put("tableName", 	tableName);
					map.put("className", 	className);
					map.put("packageName", 	$F('pakagePath'));
					map.put("jspPath", 		$F('jspPath'));
					map.put("auth", 		$F('auth'));
					map.put("systemDiv", 	$F('systemDiv'));
					
					var result = dwrUtil.invokeMap( map.toMap(), { beanId: "columnDWR", method: "makeServerSource"});
					var message = "";

					if (result) {
						for (var i = 0 ; i < tableName.length ; i++) {
							if (i != 0) 
								message += ", ";
							message += tableName[i];
						}
						alert("코드 생성이 완료되었습니다.\n" + message);
						return;
					}
					else { 
						alert("코드 생성이 실패하였습니다");
						return;
					}
	    		}
			}

			doSubmitDwr($("<c:out value="${gsMainForm}"/>"), true, userFunction);
    	},
    	doJspGenerator : function(element) {
    		var tableName = element.getAttribute("tableName");
    		var isChecked = false;
    		var map = new JMap();
   			var listColumn = new Array();
   			var insertColumn = new Array();
   			var updateColumn = new Array();
   			var elements = $("<c:out value="${gsMainForm}"/>").elements;
   			
   			for (var i = 0 ; i < elements.length ; i++) {
  				if (elements[i].type == 'checkbox' &&  elements[i].name == tableName + '_listChk' && elements[i].checked) {
  					listColumn.push(elements[i].value);
  					isChecked = true;
  				}
  				if (elements[i].type == 'checkbox' &&  elements[i].name == tableName + '_insertChk' && elements[i].checked) {
  					insertColumn.push(elements[i].value);
  					isChecked = true;
  				}
  				if (elements[i].type == 'checkbox' &&  elements[i].name == tableName + '_updateChk' && elements[i].checked) {
  					updateColumn.push(elements[i].value);
  					isChecked = true;
  				}
  			}
  			
  			if (!isChecked) {
  				alert("선택된 항목이 없습니다");
  				return;
  			} else {
  				var map = new JMap();
  				if ($('pakagePath').value == "") {
  					alert("패키지 경로항목을 선택(입력) 하십시오.");
  					$('pakagePath').focus();
  					return;
  				}
  				map.put("packageName", $F('pakagePath'));
  				map.put("tableName", tableName);
  				if (listColumn.length > 0) 
  					map.put("listColumn", listColumn);
  				if (insertColumn.length > 0)
  					map.put("insertColumn", insertColumn);
  				if (updateColumn.length > 0) 
  					map.put("updateColumn", updateColumn);
  				var result = dwrUtil.invokeMap( map.toMap(), { beanId: "columnDWR", method: "makeClientSource"});
  				if (result == true) { 
  					var message = tableName + " 테이블 참조 PageList.jsp, View.jsp, Insert.jsp, Update.jsp";
  					alert("성공적으로 " + message + " 파일이 생성이 되었습니다.");
  				}
  			}
  			
    	}
    }
    var EtcExecutor = {
    	doColumnList : function(idx, tableName) {
	        
	        isIdx = idx;
	        
	        if (isIdx != appendElement.insertElement) {
		        var map = new JMap();
				map.put("tableName", tableName);
	
				var _result = dwrUtil.invokeMap( map.toMap(), { beanId: "columnDWR", method: "getColumn"});
				
				if (_result == null || _result.length == 0) {
					alert("데이터가 없거나 네트워크 에러가 발생했습니다");
					return;
				}
				
		        var strHTML = EtcExecutor.getHtml(_result, tableName);
		        
		        var userFunction = function() {
		        	EtcExecutor.close(idx); // 닫기 버튼
		        }
	        }
	        appendElement.service('slide01',strHTML ,idx, "Top", userFunction);
		},
		close : function(idx) {
			Event.observe($('btn_close'), 'click', function() {
	            var userFunction = function(){ Element.hide(isIdx); isIdx = "";}
	            appendElement.deleteElement(userFunction);
            }, false);
		},
		doCheck : function(element) {
			var value = element.value;
			
			var check = element.checked;
			
			var elements = $("<c:out value="${gsMainForm}"/>").elements;
   			
   			for (var i = 0 ; i < elements.length ; i++) {
  				if (elements[i].type == 'checkbox' &&  elements[i].getAttribute("parent") == value) {
  					elements[i].checked = check;
  				}
  			}
		},
		parentCheck : function(element) {
			var value = element.getAttribute("parent");
			var childCheckCount = 0;
			var elements = $("<c:out value="${gsMainForm}"/>").elements;
			for (var i = 0 ; i < elements.length ; i++) {
  				if (elements[i].type == 'checkbox' &&  elements[i].getAttribute("parent") == value && elements[i].checked) {
  					childCheckCount++;
  				}
  			}
			if (childCheckCount == 0) 
				$(value+'ChkAll').checked = false;
			else 
				$(value+'ChkAll').checked = true;
		},
		getHtml : function(_result, tableName) {
			var buffer = new Array();
			buffer[buffer.length] = '<div class="inner">';

			buffer[buffer.length] = '<h4 align="left">컬럼</h4>';
			buffer[buffer.length] = '<table width="100%" class="tabletype" summary="컬럼정보에 관한 표입니다.">';
			buffer[buffer.length] = '<caption>컬럼정보</caption>';	

			buffer[buffer.length] = '<thead>';
			buffer[buffer.length] = '	<tr>';
			buffer[buffer.length] = '		<th rowspan="2">컬럼명</th>';
			buffer[buffer.length] = '		<th rowspan="2">ID</th>';
			buffer[buffer.length] = '		<th rowspan="2">PK</th>';
			buffer[buffer.length] = '		<th rowspan="2">NOT<BR/>NULL</th>';
			buffer[buffer.length] = '		<th rowspan="2">컬럼타입</th>';
			buffer[buffer.length] = '		<th rowspan="2">디폴트</th>';
			buffer[buffer.length] = '		<th rowspan="2">코멘트</th>';
			buffer[buffer.length] = '		<th>목록</th>';
			buffer[buffer.length] = '		<th>등록</th>';
			buffer[buffer.length] = '		<th>수정</th>';
			buffer[buffer.length] = '	</tr>';
			buffer[buffer.length] = '	<tr>';
			buffer[buffer.length] = '		<th class="btn"><input type="checkbox" id="listChkAll" name="listChkAll" onClick="javascript:EtcExecutor.doCheck(this)" value="list" /></th>';
			buffer[buffer.length] = '		<th class="btn"><input type="checkbox" id="insertChkAll" name="insertChkAll" onClick="javascript:EtcExecutor.doCheck(this)" value="insert" /></th>';
			buffer[buffer.length] = '		<th class="btn"><input type="checkbox" id="updateChkAll" name="updateChkAll" onClick="javascript:EtcExecutor.doCheck(this)" value="update" /></th>';
			buffer[buffer.length] = '	</tr>';
			buffer[buffer.length] = '</thead>';
			buffer[buffer.length] = '<tbody>';
			
			for (var i = 0 ; i < _result.length ; i++) {
				if (_result[i].primaryYn == "Y") {
					buffer[buffer.length] = '	<tr style="background:#FFFFEA;">';
				} else {
					buffer[buffer.length] = '	<tr>';
				}
				buffer[buffer.length] = '		<td class="left">' + _result[i].columnName + '</td>';
				buffer[buffer.length] = '		<td>' + _result[i].columnId + '</td>';
				buffer[buffer.length] = '		<td>' + _result[i].primaryYn + '</td>';
				buffer[buffer.length] = '		<td>' + _result[i].notNull + '</td>';
				buffer[buffer.length] = '		<td>' + _result[i].dataTypeInfo + '</td>';
				buffer[buffer.length] = '		<td>' + _result[i].dataDefault + '</td>';
				
				if (_result[i].columnComments == "" || _result[i].columnComments == undefined ) {
					buffer[buffer.length] = '		<td><img src="<c:out value="${gsDomainContext}" />/images/adm/common/icon/icon_x.gif" /></td>';
				} else {
					buffer[buffer.length] = '		<td class="left">' + _result[i].columnComments + '</td>';
				}
				buffer[buffer.length] = '		<td class="btn"><input type="checkbox" id="'+ _result[i].tableName +'_listChk" name="'+ _result[i].tableName +'_listChk" value="' + _result[i].columnName + '" parent="list" onClick="EtcExecutor.parentCheck(this)"; /></td>';
				buffer[buffer.length] = '		<td class="btn"><input type="checkbox" id="'+ _result[i].tableName +'_insertChk" name="'+ _result[i].tableName +'_insertChk" value="' + _result[i].columnName + '" parent="insert" onClick="EtcExecutor.parentCheck(this)"; /></td>';
				buffer[buffer.length] = '		<td class="btn"><input type="checkbox" id="'+ _result[i].tableName +'_updateChk" name="'+ _result[i].tableName +'_updateChk" value="' + _result[i].columnName + '" parent="update" onClick="EtcExecutor.parentCheck(this)"; /></td>';
				buffer[buffer.length] = '	</tr>';
			}
			
			buffer[buffer.length] = '</tbody>';
			buffer[buffer.length] = '</table>';

			buffer[buffer.length] = '<table width="100%">';
			buffer[buffer.length] = '	<tr align="center">';
			buffer[buffer.length] = '		<td class="btn_r"><input id="btn_jspGenerator" name="btn_jspGenerator" type="button" value="JSP생성" class="btn3" onClick="javascript:DwrExecutor.doJspGenerator(this);" tableName="' + tableName + '" />&nbsp;<input id="btn_close" name="btn_close" type="button" value="닫기" class="btn3"/></td>';
			buffer[buffer.length] = '	</tr>';
			buffer[buffer.length] = '</table>';
			buffer[buffer.length] = '</div>';
			
		  	return buffer.join("");
		}
    }
    var PopExecutor = {
    	doJoinTableSelect : function() {
	    	var insertUserFunction = function() {
				var result = $('ibox_iframe').contentWindow.EtcExecutor.doConfirm();
				mybox.hideIbox();
			}

			var cancelUserFunction = function() {
				mybox.hideIbox();
			}
	    	
			var selectValue = "";
			var elements = $("<c:out value="${gsMainForm}"/>").elements;
			for (var i = 0 ; i < elements.length ; i++) {
				if (elements[i].type == 'checkbox' &&  elements[i].id == "chk" && elements[i].checked) {
					if (selectValue != '')
						selectValue +=",";
					selectValue += elements[i].value;
				}
			}
	    	
	    	var input = [
	    		"selectTable=" + selectValue
	    	];
	            
            var btn = [
				{ type : 1, func : insertUserFunction },
				{ type : 3, func : cancelUserFunction }
            ];
    		mybox.service('<c:out value="${gsDomainContext}"/>/selectTableJoin.do', '조인 클래스 선택', 400, 700, 4, input, Prototype.emptyFunction, btn);
    	}
    }
</script>
